<?php

namespace app\adminapi\service;

use app\adminapi\model\log\SysOperLogDO;
use app\common\exception\OtherException;

class SysOperLogService
{
    public function addSysOperLog(array $param)
    {
        (new SysOperLogDO())->save($param);
    }

    public function list(array $param)
    {
        $where = [];
        if (isset($param['operLocation'])) {
            $where[] = ['oper_location', 'like', '%' . $param['operLocation'] . '%'];
        }
        if (isset($param['title'])) {
            $where[] = ['title', 'like', '%' . $param['title'] . '%'];
        }
        if (isset($param['operName'])) {
            $where[] = ['oper_name', 'like', '%' . $param['operName'] . '%'];
        }
        if (isset($param['status'])) {
            $where[] = ['status', '=', $param['status']];
        }
        if (isset($param['operType'])) {
            $where[] = ['oper_type', '=', $param['operType']];
        }
        $paginate['list_rows'] = $param['pageSize'];
        if (isset($param['pageNo'])) {
            $paginate['page'] = $param['pageNo'];
        }
        $result = (new SysOperLogDO())
            ->where($where)
            ->paginate($paginate)
            ->toArray();
        return [
            'total' => $result['total'],
            'list' => $result['data']
        ];
    }

    public function deleteOperLog(array $operIds)
    {
        return SysOperLogDO::destroy($operIds);
    }

    public function clearOperLog()
    {
        return SysOperLogDO::where('oper_id', '>', 0)->delete();
    }

    public function query(int $operId)
    {
        $this->validExist($operId);
        return SysOperLogDO::find($operId);
    }

    private function validExist(int $operId)
    {
        $exist = SysOperLogDO::find($operId);
        if (empty($exist)) {
            throw new OtherException("操作日志不存在");
        }
    }
}